#include "AddUserDialog.h"
#include <QDate>
#include <QMessageBox>
#include <QSqlRecord>
#include <QDebug>
#include "ui_AddUserDialog.h"
#include "Auxiliary/View/MessageBoxDialog.h"



AddUserDialog::AddUserDialog(MySqlTableModel *model, QWidget *parent) :
    QDialog(parent), model(model),
    ui(new Ui::AddUserDialog)
{
    ui->setupUi(this);
    this->setWindowFlags(Qt::FramelessWindowHint);
    ui->en_lineEdit_passwd->setEchoMode(QLineEdit::Password);
    ui->en_lineEdit_verifypasswd->setEchoMode(QLineEdit::Password);
    clearInput();
}

AddUserDialog::~AddUserDialog()
{
    delete ui;
}

int AddUserDialog::getuserCount(QString userGroup)
{
    int count = 0;
    for(int i = 0; i < model->rowCount(); ++i)
    {
        QSqlRecord record = model->record(i); 
        if(record.value("userGroup").toString() == userGroup)
        {
            count++;
        }
    }

    return count;
}


void AddUserDialog::clearInput()
{
    ui->en_lineEdit_username->clear();
    ui->ch_lineEdit_name->clear();
    ui->en_lineEdit_passwd->clear();
    ui->en_lineEdit_verifypasswd->clear();
    ui->radioButton_common->setChecked(true);
    ui->en_lineEdit_username->setFocus();
}

void AddUserDialog::on_Btn_OK_clicked()
{
    Username = ui->en_lineEdit_username->text().trimmed();
    Name = ui->ch_lineEdit_name->text();
    Passwd = ui->en_lineEdit_passwd->text();

    if(ui->radioButton_common->isChecked())
    {
        Grade = "User";
    }
    else
    {
        Grade = "Admin";
    }

    if(Username.isEmpty())
    {
        MessageBoxDialog *messageBox = new MessageBoxDialog(NULL, QString(tr("用户名不能为空。")));
        messageBox->exec();
        return;
    }
    else
    {

        for(int i = 0; i < model->rowCount(); ++i)
        {
            QSqlRecord record = model->record(i);

            if(record.isEmpty() || record.contains("userName") == false)
            {
                qDebug()<<"record is dose not contains the database field!";
                return;
            }

            if(record.value("userName").toString() == Username)
            {
                MessageBoxDialog *messageBox = new MessageBoxDialog(NULL, QString(tr("此用户已存在。")));
                messageBox->exec();
                return;
            }
        }

    }

    if(Passwd != ui->en_lineEdit_verifypasswd->text())
    {
        MessageBoxDialog *messageBox = new MessageBoxDialog(NULL, QString(tr("两次密码输入不一致。")));
        messageBox->exec();
        ui->en_lineEdit_verifypasswd->clear();
        return;
    }

    if(Grade == "Admin")
    {
        if(getuserCount("Admin") >= 10)
        {
            MessageBoxDialog *messageBox = new MessageBoxDialog(NULL, QString(tr("管理员个数已满。")));
            messageBox->exec();
            return;
        }
    }
    else if(getuserCount("User") >= 90)
    {
        MessageBoxDialog *messageBox = new MessageBoxDialog(NULL, QString(tr("普通用户个数已满。")));
        messageBox->exec();
        return;
    }

      int rowNum = model->rowCount();
      int Id = rowNum + 1;

      model->insertRow(rowNum);
      QSqlRecord  record = model->record(rowNum);

      QVector<QString> name;
      QVector<QVariant> value;
      name<<"id"<<"userName"<<"name"<<"userGroup"<<"passwd";
      value<<Id<<Username<<Name<<Grade<<Passwd;

      model->recordSetValue(&record, name, value);  //设置字段数据
      model->setRecord(rowNum, record);

      model->database().transaction();  //开始事务操作
      if(model->submitAll()){
          model->database().commit();   //提交
          qDebug()<<"commit";
      } else {
          model->database().rollback();  //回滚
          qDebug()<<"roll back";
      }

      close();
}

void AddUserDialog::on_Btn_cancel_clicked()
{
    close();
}
